package org.lagou.Dao.impl;

import java.sql.SQLException;
import java.util.List;


import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.TypeDao;
import org.lagou.bean.TypeBean;


public class TypeDaoImpl extends BaseDao implements TypeDao {

	@Override
	public List<TypeBean> getTypeFirst(int superiorid,int show1) {
		try {
			return	 query.query("select * from type where superiorid=? and show1=?", new BeanListHandler<TypeBean>(TypeBean.class),superiorid,show1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return null;
	}
	
	@Override
	public List<TypeBean> getTypeAll() {
		try {
			return	 query.query("select * from type", new BeanListHandler<TypeBean>(TypeBean.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return null;
}

	@Override
	public List<TypeBean> getTypeSecond(int superiorid) {
		try {
			return	 query.query("select * from type where superiorid=?", new BeanListHandler<TypeBean>(TypeBean.class),superiorid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return null;
	}

	@Override
	public List<TypeBean> getTypePagin(int pageIndex, int pageSize) {
		try {
			return query.query("select id,name,superiorid from type limit ?,?", new BeanListHandler<TypeBean>(TypeBean.class),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Long getTypeCount() {
		try {
			return query.query("select count(1) from type", new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
			return null;
	}

	@Override
	public int addType(TypeBean info) {
		try {
			return query.update("insert into type(name,superiorid,show1,hot) values(?,?,?,?)",info.getName(),info.getSuperiorid(),info.getShow1(),info.getHot());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updateType(TypeBean info) {
		try {
			return query.update("update type set name=?,superiorid=?,show1=?,hot=? where id=?",info.getName(),info.getSuperiorid(),info.getShow1(),info.getHot(),info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int deleteTypeById(int id) {
		try {
			return query.update("delete from type where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
}
